//
//  FiltrateUserCarView.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/9.
//

import UIKit
class FilterResultParam {
    var title: String
    var sort: NSInteger //0： 默认  1：升序 2降序
    var select: Bool? = false
    var id: NSInteger
//    var key: String
//    var key1: String
//    var key2: String
    init(title: String,
         sort: NSInteger = 0,
         select: Bool? = false,
         id: NSInteger
       ) {
        self.title = title
        self.sort = sort
        self.id = id
//        self.key = key
//        self.key1 = key1
//        self.key2 = key2
        
    }
   
}
protocol FiltrateUserCarViewDelegate{
    func didSelectFilterViewItemAction(itmes: FilterResultParam,index: NSInteger)
}

class FiltrateUserCarView: UIView {
    var collectionView: UICollectionView!
    var selectIndex = 0
    var delegate: FiltrateUserCarViewDelegate!
    
    /**
     * 水平搜索条件data
     */
    var dataArray: [FilterResultParam] = [
        FilterResultParam.init(
            title: MMLocalizedString("selling_price", comment: "售价"),
            sort: 0,
            id: 0
           ),
        FilterResultParam.init(
            title: MMLocalizedString("particular_year", comment: "年份"),
            sort: 0,
            id: 1),//降序
        FilterResultParam.init(
            title: MMLocalizedString("filtrate", comment: "筛选"),
            sort: 0,
            id: 2)
    ]
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        
        let layout = UICollectionViewFlowLayout()
        let ww = (kScreenWidth - 48)/3
        layout.itemSize = CGSize(width: ww, height: 32)
        layout.scrollDirection = .horizontal
    
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: self.frame.size.height), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.shouldHideToolbarPlaceholder = false
        self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView.register(FilterItemCell.self, forCellWithReuseIdentifier: "FilterItemCell")
        self.addSubview(self.collectionView)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension FiltrateUserCarView: UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterItemCell", for: indexPath) as! FilterItemCell
        if self.dataArray.count > indexPath.row {
            let item = self.dataArray[indexPath.row]
            cell.setSearchResultFilterView(data: item)
            
        }
        if self.selectIndex == indexPath.row{
        }else{
        }
        return cell
    }
    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        guard self.dataArray.count > indexPath.row else {
            return
        }
        if indexPath.row != 2{
            //选择
            let item = self.dataArray[indexPath.row]
            let flag = (item.sort + 1)%3 
            _ = self.dataArray.map { pObj in
                pObj.sort = 0
                pObj.select = false
            }
            item.sort = flag
            item.select = true
           
            self.collectionView.reloadData()
            self.collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
            self.delegate?.didSelectFilterViewItemAction(itmes: item, index: indexPath.row)
        }else{
            //筛选
            let item = FilterResultParam.init(title: "", sort: 0, id: 0)
            self.delegate?.didSelectFilterViewItemAction(itmes: item, index: indexPath.row)
        }
  
    }
    
}

class FilterItemCell: UICollectionViewCell {
    var bgView: UIView!
    var titleBtn: UIButton!
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.clipsToBounds = true
        self.bgView = UIView(frame: .zero)
        self.bgView.backgroundColor = UIColor(hex: 0xF6F9FC)
        self.bgView.layer.cornerRadius = 16
        self.bgView.clipsToBounds = true
        self.contentView.addSubview(self.bgView)
        
        self.titleBtn = UIButton()
        self.titleBtn.backgroundColor = UIColor.clear
        self.titleBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.titleBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.titleBtn.setImage(UIImage(named: "icon_searchResult_filter"), for: .normal)
        self.titleBtn.isUserInteractionEnabled = false
        self.titleBtn.semanticContentAttribute = .forceRightToLeft
        self.bgView.addSubview(self.titleBtn)
        
        self.addSnpLayout()
    }
    
    func addSnpLayout() {
        self.bgView.snp.makeConstraints { make in
//            make.top.equalTo(0)
//            make.left.equalTo(0)
//            make.right.equalTo(0)
//            make.height.equalTo(32)
//            make.bottom.equalTo(0)
            make.edges.equalToSuperview()
        }
        
//        titleBtn.titleLabel?.sizeToFit()
//        let tempW = titleBtn.titleLabel?.frame.size.width ?? 0
        self.titleBtn.snp.makeConstraints { make in
//            make.centerX.equalTo(self.bgView.snp.centerX)
//            make.top.equalTo(0)
//            make.height.equalTo(32)
            make.edges.equalToSuperview()
        }
    
    }
    
    func setSearchResultFilterView(data: FilterResultParam){
        self.titleBtn.setTitle(data.title, for: .normal)
        switch data.sort {
            case 1:
                self.titleBtn.setImage(UIImage(named: "icon_searchResult_filterUp"), for: .normal)
            case 2:
                self.titleBtn.setImage(UIImage(named: "icon_searchResult_filter_down"), for: .normal)
            default:
                self.titleBtn.setImage(UIImage(named: "icon_searchResult_filter"), for: .normal)
        }
        
//        if !(data.select ?? false) {
//            self.titleBtn.setImage(UIImage(named: "icon_searchResult_filter"), for: .normal)
//        }else{
//            if data.sort {
//                self.titleBtn.setImage(UIImage(named: "icon_searchResult_filterUp"), for: .normal)
//            }else{
//                self.titleBtn.setImage(UIImage(named: "icon_searchResult_filter_down"), for: .normal)
//            }
//        }
        if data.id == 2 {
            self.titleBtn.setImage(UIImage(named: "icon_filter_down"), for: .normal)
        }
        self.updateConstraints()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
